package com.tg.app.activity.device.settings;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsListBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.base.SettingData;
import com.appbase.custom.constant.CommonConstants;

import java.util.ArrayList;

public class BatterySleepActivity extends DeviceSettingsListBaseActivity {

    private ArrayList<SettingData> list = new ArrayList<>();
    private long [] sleeps  = {CommonConstants.SLEEP_LOCK_TIME_DEFAULT,10*60*1000,15*60*1000};
    private DeviceSettingsInfo  info;
    @Override
    protected void onSetupFailed() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        if (info.sleepTime==0){
            info.sleepTime = sleeps[0];
        }
        initView();
        initData();

    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle(R.string.settings_device_sleep);
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (getListView().getHeaderViewsCount() > 0) {
                    position -= getListView().getHeaderViewsCount();
                }
                setFlag(position);
                if (position<sleeps.length){
                    info.sleepTime = sleeps[position];
                    sendUpdateSettingBroadcast(info);
                }


            }
        });
        View headerview = LayoutInflater.from(this).inflate(R.layout.layout_list_textview, null);
        TextView textView = headerview.findViewById(R.id.tv_list);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
        layoutParams.topMargin = 0;
        textView.setLayoutParams(layoutParams);
        textView.setText(R.string.settings_device_sleep_tips);
        getListView().addHeaderView(headerview, null, false);

    }

    private void initData() {
        String[] weeks = getResources().getStringArray(R.array.device_sleeps);
        for (int i = 0; i < weeks.length; i++) {
            SettingData data = new SettingData(weeks[i], 0);
             if (i<sleeps.length&&info.sleepTime==sleeps[i]){
                 data.setFlag(1);
             }
            list.add(data);
        }
        setDataList(list);
        getAdapter().notifyDataSetChanged();
    }

}
